package com.example.quiz;


import java.io.ByteArrayOutputStream;

import com.example.quiz.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;



public class OrigemFoto extends Activity {
	
	private Bitmap bm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.origem_foto);
		
		
		
		Button btnTirarFoto = (Button)findViewById(R.id.btnTirarFoto);
		btnTirarFoto.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(intent, 0);
				
//				startActivity(new Intent(CriarPergunta.this, MenuPrincipal.class));				
			}
		});
		
		
		Button btnBuscarFoto = (Button)findViewById(R.id.btnBuscarFoto);
		btnBuscarFoto.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(intent, 1 );
				
//				startActivity(new Intent(CriarPergunta.this, MenuPrincipal.class));				
			}
		});
		
		Button btnVoltar = (Button)findViewById(R.id.btnVoltarCriarPergunta);
		btnVoltar.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(OrigemFoto.this, MenuPrincipal.class));				
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		super.onActivityResult(requestCode, resultCode, data);
		
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 20;
		
		if(resultCode == RESULT_OK && null != data){
			if(requestCode == 1){
				Uri selectImage = data.getData();
				String[] filePathColumn = {MediaStore.Images.Media.DATA};
				Cursor cursor = getContentResolver().query(selectImage, filePathColumn, null, null, null);
				cursor.moveToFirst();
				int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
				String picturePath = cursor.getString(columnIndex);
				cursor.close();				
		
				bm =  BitmapFactory.decodeFile(picturePath, options);	
				stream = new ByteArrayOutputStream();
				bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
			}else{
				bm = (Bitmap) data.getExtras().get("data");			
				stream = new ByteArrayOutputStream();
				bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
			}
			byte[] byteArray = stream.toByteArray();
			
			Intent intent = new Intent(OrigemFoto.this, ElaborarPergunta.class);
			intent.putExtra("foto", byteArray);
			startActivity(intent);
		}
		
		
		
	}
}
